package com.cntcdt.api.db.dao;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.cntcdt.api.db.dao.base.ConsociationCompanyUserDaoBase;
import com.cntcdt.api.db.model.ConsociationCompanyUserModel;
import com.cntcdt.api.utils.LogUtils;
import com.cntcdt.api.utils.ReflectUtils;

/**
 * 这是cdt_consociation_company_user的Dao实现类
 */
public class ConsociationCompanyUserDao extends ConsociationCompanyUserDaoBase   {
    /**
     * ConsociationCompanyUserDao对象
     */
    private static ConsociationCompanyUserDao instance = new ConsociationCompanyUserDao();

    /**
     * 默认构造方法
     */
    private ConsociationCompanyUserDao() {
    }

    /**
     * 获得对象实例
     */
    public static ConsociationCompanyUserDao getInstance() { 
         return instance;
    } 
    
    public ConsociationCompanyUserModel findByUserNameAndPassWord(String userName, String password){
    	String sql = "SELECT * FROM " + TABLE + " where userName = ? and password = ?" ;
    	List<ConsociationCompanyUserModel> modelList = queryModelList(sql , new Object[]{userName,password});
    	if(modelList.size() > 0){
    		return modelList.get(0);
    	}
        return null;
    }
    
    public ConsociationCompanyUserModel findByUserName(String userName){
    	String sql = "SELECT * FROM " + TABLE + " where userName = ? and opStatus = 0" ;
    	List<ConsociationCompanyUserModel> modelList = queryModelList(sql , new Object[]{userName});
    	if(modelList.size() > 0){
    		return modelList.get(0);
    	}
        return null;
    }
    
    /**
     * 更新操作，自动根据userModel中已经设置的字段进行更新
     * 必须要设置userModel.id属性 和 其他任意一个属性
     * 
     * 返回更新结果
     */
    public boolean updateAny(Object model){
    	StringBuffer sql = new StringBuffer("update  "+TABLE+" set ");
		List<Object> sqlArgs = new ArrayList<Object>();
		Field[] superfields = model.getClass().getSuperclass().getDeclaredFields();
//		Field[] fields = userModel.getClass().getDeclaredFields();
		for(Field field : superfields){
			if(field.getName().equals("serialVersionUID") || field.getName().equals("id")){
				continue;
			}
			if(ReflectUtils.getter(model, field.getName())!=null){
				sql.append(" "+field.getName()+"=? ,");
				sqlArgs.add(ReflectUtils.getter(model, field.getName()));
			} 
		}
		
		String sqlstr = sql.toString();
		sqlstr = sqlstr.substring(0, sqlstr.length()-1);
		sqlstr = sqlstr + " where id=?";
		sqlArgs.add(ReflectUtils.getter(model,"id"));
		LogUtils.log.info(sqlstr);
		LogUtils.log.info(sqlArgs.toString());
		
		Object[]  paramObj = sqlArgs.toArray();
        try {
             return writeDBEngine.executeUpdate(sqlstr, paramObj)>0;
        } catch(Exception e) {
           e.printStackTrace();
        }
        return false;
    }

    public ConsociationCompanyUserModel findByCompanyId(int companyId){
    	String sql = "SELECT * FROM " + TABLE + " where consociationCompanyId = ?" ;
    	List<ConsociationCompanyUserModel> modelList = queryModelList(sql , new Object[]{companyId});
    	if(modelList.size() > 0){
    		return modelList.get(0);
    	}
        return null;
    }
}
